package com.xtoad.ecms.baseinfo.converter;

import com.xtoad.ecms.baseinfo.dto.UserDTO;
import com.xtoad.ecms.baseinfo.enums.Sex;
import com.xtoad.ecms.baseinfo.model.User;
import com.xtoad.ecms.common.web.base.IConverter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户转换器实现类
 *
 * @author xtoad
 * @date 2020/05/29
 */
@Component
public class UserConverter implements IConverter<UserDTO, User> {

    /**
     * Model To DTO
     *
     * @param model 源
     * @return 目标
     */
    @Override
    public UserDTO toDto(User model) {
        UserDTO dto = new UserDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setLoginNo(model.getLoginNo());
        dto.setNickName(model.getNickName());
        dto.setEmail(model.getEmail());
        dto.setPhone(model.getPhone());
        dto.setBirthday(model.getBirthday());
        dto.setSex(model.getSex().getDesc());
        dto.setPassword(model.getPassword());
        dto.setCreateTime(model.getCreateTime());
        dto.setCreateUser(model.getCreateUser());
        dto.setLastUpdateTime(model.getLastUpdateTime());
        dto.setLastUpdateUser(model.getLastUpdateUser());
        return dto;
    }

    /**
     * DTO To Model
     *
     * @param dto 源
     * @return 目标
     */
    @Override
    public User toModel(UserDTO dto) {
        User model = new User();
        model.setId(dto.getId());
        model.setName(dto.getName());
        model.setLoginNo(dto.getLoginNo());
        model.setNickName(dto.getNickName());
        model.setEmail(dto.getEmail());
        model.setPhone(dto.getPhone());
        model.setBirthday(dto.getBirthday());
        model.setSex(Sex.valueOf(dto.getSex()));
        model.setPassword(dto.getPassword());
        model.setCreateTime(dto.getCreateTime());
        model.setCreateUser(dto.getCreateUser());
        model.setLastUpdateTime(dto.getLastUpdateTime());
        model.setLastUpdateUser(dto.getLastUpdateUser());
        return model;
    }

    /**
     * 批量Model To DTO
     *
     * @param models 源
     * @return 目标
     */
    @Override
    public List<UserDTO> toDtoList(List<User> models) {
        List<UserDTO> dtoList = new ArrayList<>();
        models.forEach(model -> dtoList.add(this.toDto(model)));
        return dtoList;
    }

    /**
     * 批量DTO To Model
     *
     * @param dtos 源
     * @return 目标
     */
    @Override
    public List<User> toModelList(List<UserDTO> dtos) {
        List<User> modelList = new ArrayList<>();
        dtos.forEach(dto -> modelList.add(this.toModel(dto)));
        return modelList;
    }
}
